local ServerOHOH = _G.ServerOHOH;
local M = ServerOHOH.class.new(script, ServerOHOH.files.Server_PlayerComponent(),script.Parent.Name);

local FriendBoost=ServerOHOH.config.Game.FriendBoost
local ConnectClass=_G.CommonOHOH.load.Connect();

function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);

    self.myUserId=self.parent.main_inst.UserId
    local boost=Instance.new("NumberValue",self.parent.main_inst)
    boost.Name="FriendBoost"
    boost.Value= self:computeBoost()
    boost.Changed:Connect(function(value)
        
    end)
	self.FriendBoostEvent=ConnectClass.new()--攻击

    self.boost=boost

    self.playerAddConnect=game.Players.PlayerAdded:Connect(function(player)
        self.boost.Value=self:computeBoost()
   end)

   self.playerRemoveConnect= game.Players.PlayerRemoving:Connect(function(player)
        assert(self.boost,"self.boost==null")
        self.boost.Value=self:computeBoost()
   end)

    self.parent.ExpValueMul2Event:Connect(function()
        return 1+self.boost.Value
    end)
end;

function M.dtor(self)
    if self.playerAddConnect then
        self.playerAddConnect:Disconnect()
        self.playerAddConnect=nil
    end

    if self.playerRemoveConnect then
        self.playerRemoveConnect:Disconnect()
        self.playerRemoveConnect=nil
    end

	M.super.dtor(self);
end;

function M.computeBoost(self)
    local s, d = xpcall(function()
        local boost=0
        for k,v in pairs(game.Players:GetPlayers()) do
            if v.UserId~=self.myUserId and v:IsFriendsWith(self.myUserId) then
                boost=boost+FriendBoost
            end
        end
        return boost
    end, debug.traceback);

    if s then
        return d
    end

    return 0
end

return M
